package com.fy.fyspace.controller;

import com.fy.fyspace.common.annotation.GlobalInterceptor;
import com.fy.fyspace.common.result.R;
import com.fy.fyspace.constant.ResultMessageConstant;
import com.fy.fyspace.ex.handler.ExHandler;
import com.fy.fyspace.mapper.UserFeedBackMapper;
import com.fy.fyspace.model.dto.user.UserFeedbackDTO;
import com.fy.fyspace.model.dto.user.UserLoginDTO;
import com.fy.fyspace.model.dto.user.UserRegisterDTO;
import com.fy.fyspace.model.entity.UserFeedback;
import com.fy.fyspace.model.vo.user.LoginUserVO;
import com.fy.fyspace.model.vo.user.UserStatisticsVO;
import com.fy.fyspace.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

import static com.fy.fyspace.common.result.R.ok;
import static com.fy.fyspace.constant.KeysConstant.LOGIN_SESSION_KEY;

@Api(tags = "用户模块接口")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @Resource
    private UserFeedBackMapper userFeedBackMapper;

    @ApiOperation(value = "用户注册", notes = "注册新用户，需要提供账号和密码")
    @PostMapping("/register")
//    @PerformanceMonitor(value = "用户注册", description = "监控用户注册接口性能", slowThreshold = 3000, logParams = false, logResult = false)
    public R<Long> userRegister(
            @ApiParam(value = "用户注册信息", required = true) @RequestBody UserRegisterDTO userRegisterDTO) {
        return ok(userService.userRegister(userRegisterDTO));
    }

    @ApiOperation(value = "用户登录", notes = "使用账号密码登录系统")
    @PostMapping("/login")
    public R<LoginUserVO> userLogin(
            @ApiParam(value = "用户登录信息", required = true) @RequestBody UserLoginDTO userLoginDTO,
            HttpSession httpSession) {
        return ok(userService.userLogin(userLoginDTO));
    }

    @ApiOperation(value = "退出登录", notes = "清除用户登录状态")
    @PostMapping("/logout")
    public R<Boolean> userLogout(HttpSession httpSession) {
        httpSession.removeAttribute(LOGIN_SESSION_KEY);
        return ok();
    }

    @ApiOperation(value = "获取当前登录用户", notes = "获取当前登录用户的详细信息")
    @GetMapping("/current")
    @GlobalInterceptor
    public R<LoginUserVO> getCurrentUser(HttpSession httpSession) {
        return ok(userService.getLoginUser());
    }

    @ApiOperation(value = "上传用户头像", notes = "上传并更新用户的头像图片")
    @PostMapping("/changeAvatar")
    @GlobalInterceptor
    public R uploadAvatar(
            @ApiParam(value = "头像文件", required = true) @RequestPart("file") MultipartFile file,
            HttpSession httpSession) {
        return super.uploadAvatar(file, httpSession);
    }

    @ApiOperation(value = "修改密码", notes = "修改用户登录密码，需要提供旧密码和新密码")
    @PostMapping("/updatePassword")
    @GlobalInterceptor
    public R updatePassword(
            @ApiParam(value = "旧密码", required = true) @RequestParam String oldPassword,
            @ApiParam(value = "新密码", required = true) @RequestParam String newPassword,
            HttpSession httpSession) {
        return ok(userService.updatePassword(oldPassword, newPassword));
    }

    @ApiOperation(value = "修改用户名称", notes = "修改用户的显示名称")
    @PostMapping("/updateName")
    @GlobalInterceptor
    public R updateUserName(
            @ApiParam(value = "新的用户名称", required = true) @RequestParam String userName,
            HttpSession httpSession) {
        return ok(userService.updateUserName(userName));
    }

    @ApiOperation(value = "修改用户简介", notes = "修改用户的个人简介信息")
    @PostMapping("/updateProfile")
    @GlobalInterceptor
    public R updateUserProfile(
            @ApiParam(value = "新的用户简介", required = true) @RequestParam String userProfile,
            HttpSession httpSession) {
        return ok(userService.updateUserProfile(userProfile));
    }

    @ApiOperation(value = "获取用户统计信息", notes = "获取用户的图片、空间和活动统计信息")
    @GetMapping("/statistics")
    @GlobalInterceptor
    public R<UserStatisticsVO> getUserStatistics(HttpSession httpSession) {
        return ok(userService.getUserStatistics());
    }

    @ApiOperation(value = "重置密码", notes = "通过邮箱验证码重置密码")
    @PostMapping("/resetPassword")
    public R resetPassword(
            @ApiParam(value = "邮箱", required = true) @RequestParam String email,
            @ApiParam(value = "新密码", required = true) @RequestParam String password,
            @ApiParam(value = "确认密码", required = true) @RequestParam String confirmPassword,
            @ApiParam(value = "邮箱验证码", required = true) @RequestParam String emailCode,
            HttpSession httpSession) {
        return ok(userService.resetPassword(email, password, confirmPassword, emailCode));
    }

    /**
     * 获取用户邮箱
     *
     * @return
     */
    @ApiOperation(value = "获取用户邮箱", notes = "获取用户邮箱")
    @GetMapping("/getUserEmail")
    public R getUserEmail() {
        return ok(userService.getUserEmail());
    }

    /**
     * 修改用户邮箱
     *
     * @param email
     * @return
     */
    @ApiOperation(value = "修改用户邮箱", notes = "修改用户的邮箱")
    @PostMapping("/updateUserEmail")
    public R updateUserEmail(@RequestParam String email) {

        return ok(userService.updateUserEmail(email));
    }
//    /**
//     * 销毁用户
//     * @param userId
//     * @return
//     */
//    @ApiOperation(value = "销毁用户", notes = "销毁用户")
//    @PostMapping("/destroy/{userId}")
//    @GlobalInterceptor
//    public R destroyUser(@PathVariable Long userId) {
//        return ok(userService.destroyUser(userId));
//    }

    /**
     * 用户提交反馈
     *
     * @param userFeedbackDTO
     * @return
     */
    @PostMapping("/feedback/submit")
    @ApiOperation(value = "提交反馈", notes = "用户提交反馈")
    public R submitFeedback(@RequestBody UserFeedbackDTO userFeedbackDTO) {
        ExHandler.throwIf(userFeedbackDTO == null, ResultMessageConstant.PARAMS_ERROR);

        UserFeedback userFeedback = new UserFeedback();
        if (userFeedbackDTO.getType() != null)
            userFeedback.setType(userFeedbackDTO.getType());
        if (userFeedbackDTO.getDescription() != null)
            userFeedback.setDescription(userFeedbackDTO.getDescription());
        if (userFeedbackDTO.getContact() != null)
            userFeedback.setContact(userFeedbackDTO.getContact());
        userFeedback.setCreateTime(new Date());
        userFeedback.setIsRead(0);
        userFeedBackMapper.insertOrUpdate(userFeedback);
        return ok();
    }
}
